﻿using System;
using System.ComponentModel;
using System.Configuration.Install;
using System.Windows.Forms.Design;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using System.Security.Permissions;
using System.Windows.Forms;
using System.Text;
using System.Linq;
using Microsoft.Win32;
using CommonUtils;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;


namespace FreeTranslator
{
    using MyTextImport = Lazy<ITextImport, ITextImportMetaData>;
    [RunInstaller(true)]
    public partial class FreeTranslatorInstall : System.Configuration.Install.Installer
    {
        //public static readonly  string[] fileExtensions = new string[] { ".srt", ".sub", ".pdf", ".txt", ".doc", ".html", ".rtf" };
        public FreeTranslatorInstall()
        {
            string logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "LanguageTranslatorInstall.log");
            Trace.Listeners.Add(new TextWriterTraceListener(logPath));
            Trace.TraceInformation("FreeTranslatorInstall initialized at {0}, Win ver = {1}, Crt dir: '{2}', CLR version ={3}, Is64BitProcess = {4}, Is64BitOperatingSystem = {5}",
                DateTime.Now, Environment.OSVersion.Version, Environment.CurrentDirectory, RuntimeEnvironment.GetSystemVersion(), Environment.Is64BitProcess, Environment.Is64BitOperatingSystem);
            InitializeComponent();
            //Trace.TraceInformation("");
        }
        // System.Diagnostics.Trace.TraceInformation("FreeTranslatorInstall Disposed at {0}, ", System.DateTime.Now );
        private void FreeTranslatorInstall_AfterInstall(object sender, InstallEventArgs e)
        {

            try
            {
                Trace.TraceInformation("FreeTranslatorInstall_AfterInstall: sender {0}, InstallEventArgs {1}", sender, e);
            }
            catch (Exception ex)
            {
                Trace.TraceError("'{0}' failed to start because {1}", ex.StackTrace, ex.Message);
            }
            finally
            {
                Trace.Flush();

            }
        }

        private void FreeTranslatorInstall_AfterRollback(object sender, InstallEventArgs e)
        {

            try
            {
                Trace.TraceInformation("FreeTranslatorInstall_AfterRollback: sender {0}, InstallEventArgs {1}", sender, e);
            }
            catch (Exception ex)
            {
                Trace.TraceError("'{0}' failed to start because {1}", ex.StackTrace, ex.Message);
            }
            finally
            {
                Trace.Flush();

            }
        }

        private void FreeTranslatorInstall_AfterUninstall(object sender, InstallEventArgs e)
        {

            try
            {
                Trace.TraceInformation("FreeTranslatorInstall_AfterUninstall: sender {0}, InstallEventArgs {1}", sender, e);
            }
            catch (Exception ex)
            {
                Trace.TraceError("'{0}' failed to start because {1}", ex.StackTrace, ex.Message);
            }
            finally
            {
                Trace.Flush();

            }
        }

        private void FreeTranslatorInstall_BeforeInstall(object sender, InstallEventArgs e)
        {

            try
            {
                Trace.TraceInformation("FreeTranslatorInstall_BeforeInstall: sender {0}, InstallEventArgs {1}", sender, e);
            }
            catch (Exception ex)
            {
                Trace.TraceError("'{0}' failed to start because {1}", ex.StackTrace, ex.Message);
            }
            finally
            {
                Trace.Flush();

            }
        }

        private void FreeTranslatorInstall_BeforeRollback(object sender, InstallEventArgs e)
        {

            try
            {
                Trace.TraceInformation("FreeTranslatorInstall_BeforeRollback: sender {0}, InstallEventArgs {1}", sender, e);
            }
            catch (Exception ex)
            {
                Trace.TraceError("'{0}' failed to start because {1}", ex.StackTrace, ex.Message);
            }
            finally
            {
                Trace.Flush();

            }
        }
         internal static readonly string[] fileExtensions = new string[] { ".srt", ".sub", ".pdf", ".txt", ".doc", ".html", ".rtf" };
         ITextImportController PlugInController
         {
             get
             {
                 return MEFController.PlugInImportController;
             }
         }
        private void FreeTranslatorInstall_BeforeUninstall(object sender, InstallEventArgs e)
        {
            try
            {

                Trace.TraceInformation("FreeTranslatorInstall_BeforeUninstall: sender {0}, InstallEventArgs {1}", sender, e);
                if (PlugInController == null)
                {
                    Trace.TraceError("'Cant't register extensions because the Contoller is null");
                    return;
                }
                foreach (string item in fileExtensions)
                {
                    MyTextImport fri = PlugInController[item];
                    ShellIntegration.Unregister(fri.Metadata.FileExtension, _shellRegMenuName);
                }
                ShellIntegration.ShellRefresh();
                Trace.TraceInformation("Uninstalled {0} extentsions", fileExtensions.Length);
            }
            catch (Exception ex)
            {
                Trace.TraceError("'{0}' failed to start because {1}", ex.StackTrace, ex.Message);
            }
            finally
            {
                Trace.Flush();

            }
        }

        static internal readonly string _shellRegMenuName = Assembly.GetExecutingAssembly().GetName().Name + "MenuEntry";
        private void FreeTranslatorInstall_Committed(object sender, InstallEventArgs e)
        {

            try
            {

                Trace.TraceInformation("FreeTranslatorInstall_Committed: sender {0}, InstallEventArgs {1}", sender, e);
                if (PlugInController == null)
                {
                    Trace.TraceError("'Cant't register extensions because the Contoller is null");
                    return;
                }
                foreach (string item in fileExtensions)
                {
                    MyTextImport fri = PlugInController[item];
                    ShellIntegration.Register(fri.Metadata, _shellRegMenuName);
                }
                ShellIntegration.ShellRefresh();
                 
                Trace.TraceInformation("Installed {0} extentsions", fileExtensions.Length);
            }
            catch (Exception ex)
            {
                Trace.TraceError("'{0}' failed to start because {1}", ex.StackTrace, ex.Message);
            }
            finally
            {
                Trace.Flush();

            }
        }

        private void FreeTranslatorInstall_Committing(object sender, InstallEventArgs e)
        {

            try
            {
                Trace.TraceInformation("FreeTranslatorInstall_Committing: sender {0}, InstallEventArgs {1}", sender, e);
            }
            catch (Exception ex)
            {
                Trace.TraceError("'{0}' failed to start because {1}", ex.StackTrace, ex.Message);
            }
            finally
            {
                Trace.Flush();

            }
        }
    }
}
